package com.jw.banddroid.bandsintown;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.jw.banddroid.bandsintown.Event;
import com.jw.banddroid.bandsintown.Artist;
import com.jw.banddroid.bandsintown.Storage;

public class ApiParser extends DefaultHandler {

	/**
	 * Retrving the text node of an element
	 */
	private String textnode;
	
	private List<Artist> artists;
	
	private Artist artist;
	
	/**
	 * Boolean value to find out wheater the "name" element from the API is an
	 * Artist or an Venue.
	 */
	private boolean isArtist;
	
	private Event event;
	
	private Storage storage;

	/**
	 * Constructor for the ApiParser
	 */
	public ApiParser() {
		super();
		textnode = "";
		isArtist = false;
		
		storage = new Storage();
	}

	/**
	 * Triggers the beginning of parsing.
	 * 
	 * @throws SAXException
	 * @throws IOException when the xmlRetriever could not provide any data.
	 */
	public void parseDocument(final InputSource in) {
		try {
			SAXParserFactory factory = SAXParserFactory.newInstance();
			factory.setValidating(true);
			SAXParser saxParser = factory.newSAXParser();
			saxParser.parse(in, this);
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	}

	public void startElement(String uri, String name, String qName,
			Attributes attributes) throws SAXException {
		if (qName.equalsIgnoreCase("event")) {
			event = new Event();
			artists = new ArrayList<Artist>(); 
		} else if (qName.equalsIgnoreCase("artist")) {
			isArtist = true;
			artist = new Artist();
		}
	}

	public void characters(char[] ch, int start, int length)
			throws SAXException {
		textnode = new String(ch, start, length);
	}

	public void endElement(String uri, String name, String qName)
			throws SAXException {
		if (qName.equalsIgnoreCase("name")) {
			if (isArtist == true) {
				System.out.println("Artist: " + textnode);
				artist.setName(textnode);
			} else {
				System.out.println("Venue: " + textnode);
			}
		} else if (qName.equalsIgnoreCase("datetime")) {
			event.setDatetime(textnode);
			System.out.println("Dato: " + textnode);
		} else if (qName.equalsIgnoreCase("city")) {
			System.out.println("City: " + textnode);
		} else if (qName.equalsIgnoreCase("latitude")) {
			System.out.println("Latitude: " + textnode);
		} else if (qName.equalsIgnoreCase("longitude")) {
			System.out.println("Longitude: " + textnode);
		} else if (qName.equalsIgnoreCase("ticket_status")) {

			System.out.println("Tickets: " + textnode);
		} else if (qName.equalsIgnoreCase("ticket_url")) {
			System.out.println("TicketUrl: " + textnode);
		} else if (qName.equalsIgnoreCase("artist")) {
			isArtist = false;
			artists.add(artist);
		} else if (qName.equalsIgnoreCase("event")) {
			store(event);
		}
	}
	
	public void store(Event event2) {
		storage.storeEvent(event2);
	}
	
	public Storage getStorage() {
		return storage;
	}
}